package com.aimuceliang.aimuceliang.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.aimuceliang.aimuceliang.Constants;
import com.aimuceliang.aimuceliang.bean.TiChiBean;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.green_dao.TiChiCeLiang;
import com.aimuceliang.aimuceliang.green_dao.TiChiCeLiangDaoUtils;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.ui.adapter.CommonAdapter;
import com.aimuceliang.aimuceliang.ui.adapter.CommonViewHolder;
import com.aimuceliang.aimuceliang.utils.BluetoothChat;
import com.aimuceliang.aimuceliang.utils.DateUtils;
import com.aimuceliang.aimuceliang.utils.XLog;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.OnClick;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TiChiActivity extends BaseActivity {

    private static final int CANCLE_MESSAGE = 0;

    private static final int NEXT_MESSAGE = 1;

    @BindView(R.id.tc_earid)
    EditText etEarid;
    @BindView(R.id.tc_weight)
    EditText tc_weight;
    @BindView(R.id.tc_height)
    EditText tc_height;
    @BindView(R.id.tc_xc)
    EditText tc_xc;
    @BindView(R.id.tc_tk)
    EditText tc_tk;
    @BindView(R.id.tc_xw)
    EditText tc_xw;
    @BindView(R.id.tc_tdh)
    EditText tc_tdh;
    @BindView(R.id.tc_gw)
    EditText tc_gw;
    @BindView(R.id.tc_xs)
    EditText tc_xs;
    @BindView(R.id.tc_wk)
    EditText tc_wk;
    @BindView(R.id.tc_wc)
    EditText tc_wc;
    @BindView(R.id.tc_szbk)
    EditText tc_szbk;
    @BindView(R.id.listView)
    ListView listView;

    private TiChiCeLiangDaoUtils daoUtils;
    private MyAdapter adapter;
    private ArrayList<TiChiCeLiang> tiChiCeLiangs;

    private String strTime = null;
    private String strStaNum = "0";

    private Handler mHandler = new Handler(){
        @Override public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case CANCLE_MESSAGE :
                    // 发送清除数据命令
                    byte[] data_clean = {0x01 ,0x06 ,0x10 , (byte) 0xD1,0x00 ,0x00 , (byte) 0xDD,0x33};
                    BluetoothChat.getBluetoothChat(0).sendMessageHandle(data_clean);
                    break;
                case NEXT_MESSAGE :
                    // 发送获取下一条数据命令
                    byte[] data_get = {0x01,0x03,0x10, (byte) 0xCC,0x00,0x14, (byte) 0x81,0x3A};
                    BluetoothChat.getBluetoothChat(0).sendMessageHandle(data_get);
                    break;
            }
        }
    };

    @Override
    protected int initContentView() {
        return R.layout.activity_tichiceliang;
    }

    @Override
    protected void initUIAndListener() {
        setTitle("体尺测量");
        Button mBtnNext = (Button) findViewById(R.id.tc_next_btn);
        mBtnNext.setEnabled(false);

        daoUtils = new TiChiCeLiangDaoUtils(this);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        BluetoothChat.getBluetoothChat(0).shutdownClient();
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {
        mHandler.sendEmptyMessageDelayed(NEXT_MESSAGE,1000);//2s 后发送下一只
        tiChiCeLiangs = new ArrayList<>();
        tiChiCeLiangs.addAll(daoUtils.queryAllTiChiCeLiang());
        while(tiChiCeLiangs.size() > 3) {
            tiChiCeLiangs.remove(0);
        }
        Collections.reverse(tiChiCeLiangs);
        adapter.bindData(tiChiCeLiangs);
    }

    @OnClick({R.id.tc_next_btn, R.id.tc_data_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tc_next_btn:
                String earid = etEarid.getText().toString().trim();
                String weight = tc_weight.getText().toString().trim();
                String height = tc_height.getText().toString().trim();
                String txc = tc_xc.getText().toString().trim();
                String tk = tc_tk.getText().toString().trim();
                String xw = tc_xw.getText().toString().trim();
                String tdh = tc_tdh.getText().toString().trim();
                String gw = tc_gw.getText().toString().trim();
                String xs = tc_xs.getText().toString().trim();
                String wc = tc_wc.getText().toString().trim();
                String szbk = tc_szbk.getText().toString().trim();
                String wk = tc_wk.getText().toString().trim();
                if (earid.isEmpty()||weight.isEmpty()||height.isEmpty()||tdh.isEmpty()||txc.isEmpty()) {
                    Toast.makeText(this, "数据为空", Toast.LENGTH_SHORT).show();
                    //return;
                } else {
                    TiChiCeLiang tiChiCeLiang = new TiChiCeLiang();
                    tiChiCeLiang.setEarid(earid);
                    tiChiCeLiang.setWeight(weight);
                    tiChiCeLiang.setHeight(height);
                    tiChiCeLiang.setChannelid(tdh);
                    tiChiCeLiang.setGuanwei(gw);
                    tiChiCeLiang.setTenlength(szbk);
                    tiChiCeLiang.setLength(txc);
                    tiChiCeLiang.setWeichang(wc);
                    tiChiCeLiang.setXiongwei(xw);
                    tiChiCeLiang.setXiongshen(xs);
                    tiChiCeLiang.setTixiechang(txc);
                    tiChiCeLiang.setWeikuan(wk);
                    tiChiCeLiang.setLength(tk);
                    tiChiCeLiang.setId(null);
                    tiChiCeLiang.setDeviceid(Constants.DEVICE_ID);
                    //tiChiCeLiang.setTime(DateUtils.getCurrentFormatTime());
                    tiChiCeLiang.setTime(strTime);
                    strTime = null;
                    boolean isInsertSuccess = daoUtils.insertTiChiCeLiang(tiChiCeLiang);
                    if (isInsertSuccess) {
                        etEarid.setText("");
                        tc_weight.setText("");
                        tc_height.setText("");
                        tc_xc.setText("");
                        tc_tk.setText("");
                        tc_xw.setText("");
                        tc_tdh.setText("");
                        tc_gw.setText("");
                        tc_xs.setText("");
                        tc_wc.setText("");
                        tc_szbk.setText("");
                        tc_wk.setText("");
                        tiChiCeLiangs.add(tiChiCeLiang);

                        while(tiChiCeLiangs.size() > 3) {
                            tiChiCeLiangs.remove(0);
                        }
                        Collections.reverse(tiChiCeLiangs);
                        adapter.notifyDataSetChanged();
                    }
                }

                Button mBtnNext = (Button) findViewById(R.id.tc_next_btn);
                mBtnNext.setEnabled(false);

                if(strStaNum.equals("1")) {
                    mHandler.sendEmptyMessage(CANCLE_MESSAGE);//清除
                }
                mHandler.sendEmptyMessageDelayed(NEXT_MESSAGE,1000);//5s 后发送下一只

                break;

            case R.id.tc_data_btn:
                 startActivityForResult(new Intent(TiChiActivity.this, TCDataActivity.class), 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(1 == resultCode) {
            tiChiCeLiangs.clear();
            adapter.notifyDataSetChanged();
        }
    }

    // Called in Android UI's main thread
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessage(TiChiBean event) {
        TiChiCeLiang tiChiCeLiang = new TiChiCeLiang();
        tiChiCeLiang.setEarid(event.strEarNum);
        tiChiCeLiang.setWeight(event.strWeight);
        tiChiCeLiang.setHeight(event.strHeight);
        tiChiCeLiang.setLength(event.strLength);
        tiChiCeLiang.setXiongshen(event.strChest);
        tiChiCeLiang.setWeikuan(event.strTime);
        tiChiCeLiang.setLength(event.strStaNum);
        tiChiCeLiang.setTime(event.strTime);

        strTime = event.strTime;
        strStaNum = event.strStaNum;

        Button mBtnNext = (Button) findViewById(R.id.tc_next_btn);
        if(strStaNum.equals("1")) {
            etEarid.setText(event.strEarNum);
            tc_weight.setText(event.strWeight);
            tc_height.setText(event.strHeight);
            tc_xc.setText(event.strLength);
            tc_tk.setText("暂无");
            tc_xw.setText(event.strChest);
            tc_tdh.setText("暂无");
            tc_gw.setText("暂无");
            tc_xs.setText("暂无");
            tc_wk.setText("暂无");
            tc_wc.setText("暂无");
            tc_szbk.setText("暂无");

            mBtnNext.setEnabled(true);
        } else {
            mBtnNext.setEnabled(false);
            mHandler.sendEmptyMessageDelayed(NEXT_MESSAGE,5000);//5s 后发送下一只
        }
    }

    private class MyAdapter extends CommonAdapter<TiChiCeLiang> {

        MyAdapter() {
            super(R.layout.item_tichi_measure, TiChiActivity.this);
        }

        @Override
        public void convert(CommonViewHolder holder, TiChiCeLiang tiChiCeLiang) {
            //初始化点击事件,防止复用导致点击出错
            holder.getView(R.id.tc_delete).setOnClickListener(null);
            holder.setText(R.id.tc_ear_id, tiChiCeLiang.getEarid());
            holder.setText(R.id.tc_weight, tiChiCeLiang.getWeight());
            holder.setText(R.id.tc_txc, tiChiCeLiang.getTixiechang());
            holder.setText(R.id.tc_tg, tiChiCeLiang.getHeight());

            holder.getView(R.id.tc_delete).setOnClickListener(v -> {
                daoUtils.deleteTiChiCeLiang(tiChiCeLiang);
                tiChiCeLiangs.clear();
                tiChiCeLiangs.addAll(daoUtils.queryAllTiChiCeLiang());
                while(tiChiCeLiangs.size() > 3) {
                    tiChiCeLiangs.remove(0);
                }
                Collections.reverse(tiChiCeLiangs);
                adapter.notifyDataSetChanged();
            });
        }
    }

}
